package com.hgd.security.evp.biz.websocket;

import com.hgd.security.evp.biz.websocket.hgd.handler.AbstractHGDChannelInboundHandler;
import io.netty.channel.Channel;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelId;
import io.netty.channel.group.ChannelGroup;
import io.netty.channel.group.ChannelMatcher;
import io.netty.channel.group.ChannelMatchers;
import io.netty.channel.group.DefaultChannelGroup;
import io.netty.handler.codec.http.websocketx.TextWebSocketFrame;
import io.netty.util.concurrent.GlobalEventExecutor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;

/**
 * @author By 尚
 * @date 2019/4/6 20:46
 */
@Component
@Slf4j
public class ChatHandler extends AbstractHGDChannelInboundHandler<TextWebSocketFrame> {

    // 用于记录和管理所有客户端的channle
    private static ChannelGroup clients =
        new DefaultChannelGroup(GlobalEventExecutor.INSTANCE);

    private static Map<String,Channel> channelIdChannelMap=
        new HashMap<>();

    @Override
    protected void channelRead0(ChannelHandlerContext ctx, TextWebSocketFrame msg) throws Exception {
        // 获取客户端传输过来的消息
        String content = msg.text();
        log.debug("接受到的数据：" + content);

//        for (Channel channel : clients) {
//            channel.writeAndFlush(new TextWebSocketFrame("[服务器在]" + LocalDateTime.now() + "接受到消息, 消息为：" + content));
//        }
    }

    @Override
    public void channelWriteAndFlush(String msg) {
        clients.writeAndFlush(new TextWebSocketFrame(msg));
    }

    @Override
    public void channelWriteAndFlush(String msg, String channelId) {
        Channel channel = channelIdChannelMap.get(channelId);
        channel.writeAndFlush(new TextWebSocketFrame(msg));
    }

    @Override
    protected void addChannel(Channel channel) {
        ChatHandler.clients.add(channel);
        addChannelIdChannelMap(channel.id().asShortText(), channel);
    }

    @Override
    protected void addChannelIdChannelMap(String channelId,Channel channel) {
        channelIdChannelMap.put(channelId, channel);
    }

    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {
    }

    @Override
    public void handlerAdded(ChannelHandlerContext ctx) throws Exception {
        addChannel(ctx.channel());
        log.debug(ctx.channel().id()+"添加成功");
    }

    @Override
    public void handlerRemoved(ChannelHandlerContext ctx) throws Exception {
        log.debug("客户端断开，channle对应的长id为："
            + ctx.channel().id().asLongText());
    }

    @Override
    public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        ctx.close();
        super.exceptionCaught(ctx, cause);
    }

//    public void setdMsg(String msg){
//        for (Channel channel : super.getClients()) {
//            channel.writeAndFlush(new TextWebSocketFrame(msg));
//        }
//    }
}
